import { RequestHandler } from 'express'
import { failure, success } from '@/utils/response'
import roleService from '@/services/roleService'

const getRoles: RequestHandler = async (req, res) => {
  try {
    const data = await roleService.getRoles()
    success(res, '查询角色列表成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询角色列表失败：')
  }
}

const getRoleInfo: RequestHandler = async (req, res) => {
  try {
    const data = await roleService.getRoleInfo(+req.params.id)
    success(res, '查询角色成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询角色失败：')
  }
}

const addRole: RequestHandler = async (req, res) => {
  try {
    const data = await roleService.addRole(req.body)
    success(res, '添加角色成功。', data)
  } catch (error) {
    failure(res, error as Error, '添加角色失败：')
  }
}

const updateRole: RequestHandler = async (req, res) => {
  try {
    const data = await roleService.updateRole(+req.params.id, req.body)
    success(res, '修改角色成功。', data)
  } catch (error) {
    failure(res, error as Error, '修改角色失败：')
  }
}

const deleteRole: RequestHandler = async (req, res) => {
  try {
    await roleService.deleteRole(+req.params.id)
    success(res, '删除角色成功。')
  } catch (error) {
    failure(res, error as Error, '删除角色失败：')
  }
}

export default {
  getRoles,
  getRoleInfo,
  addRole,
  updateRole,
  deleteRole,
}
